package net.net23.sysdent.client.ui.widgets.model;

import net.net23.sysdent.client.controller.AppController;
import net.net23.sysdent.client.ui.widgets.search.ResultTable;

import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class SearcherWidget extends ContentWidget {

	private DockPanel dock;
	private ResultTable result;
	private ScrollPanel scroller;
	private HorizontalPanel overlay;

	public SearcherWidget(String key, ImageResource icon) {
		super(key, icon);
		dock = new DockPanel();
		dock.getElement().setId("searcher-layout");
		dock.setWidth("100%");
		Widget criteriaWidget = getCriteriaWidget();
		if (criteriaWidget != null) {
			dock.add(criteriaWidget, DockPanel.NORTH);
		}
		result = getResultWidget();
		if (result != null) {
			overlay = new HorizontalPanel();
			overlay.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			overlay.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

			overlay.setStyleName("t4l-overlay");
			overlay.setVisible(false);
			scroller = new ScrollPanel();
			scroller.setWidth("100%");
			scroller.add(result);
			dock.add(scroller, DockPanel.CENTER);
		}
		sinkEvents(Event.ONKEYUP);
		initWidget(dock);
	}

	@Override
	protected void onLoad() {
		performSearch();
	}

	public void freeze() {
		if (result != null) {
			int left = result.getAbsoluteLeft();
			int top = result.getAbsoluteTop();
			int width = result.getOffsetWidth();
			int height = Window.getClientHeight() - top;
			Element elem = overlay.getElement();
			DOM.setStyleAttribute(elem, "position", "absolute");
			DOM.setStyleAttribute(elem, "left", String.valueOf(left));
			DOM.setStyleAttribute(elem, "top", String.valueOf(top));
			DOM.setStyleAttribute(elem, "width", String.valueOf(width));
			DOM.setStyleAttribute(elem, "height", String.valueOf(height));
			DOM.setStyleAttribute(elem, "zIndex", "100");
			overlay.setVisible(true);
			Image loading = new Image(AppController.images.loading());
			overlay.clear();
			overlay.add(loading);
			RootPanel.get().add(overlay);
		}
	}
	
	public void onBrowserEvent(Event event) {
		switch (DOM.eventGetType(event)) {
		case Event.KEYEVENTS: {
			System.out.println("presiono alguna tecla");
			break;
		}
		}
	}

	public void defreeze() {
		overlay.setVisible(false);
		RootPanel.get().remove(overlay);
	}

	public abstract void performSearch();

	public abstract Widget getCriteriaWidget();

	public abstract ResultTable getResultWidget();

	public ResultTable getResult() {
		return result;
	}
}
